package eerr.llrr.oorr.pte9983.b.pl190.host668.notification;

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import eerr.llrr.oorr.pte9983.b.base_api_keep.BaseApplication

/**
 * Created by ZhangDeCheng on 2023/6/27
 */
private const val TAG = "Notification_M"

@SuppressLint("StaticFieldLeak")
object MyNotificationManager {

    const val NOTIFY_DELETE_ACTION = "com.notification.desktop.show"
    const val NOTIFY_CLICK_ACTION = "com.cleanmore.home.HomeActivity.from.notifiy"

    private const val ALL_COUNT = 3

    private var count = 0
    private var index = 0

    private val handler: Handler = MyHandler(Looper.getMainLooper())

    private var mainContext: Context? = null
    private var isRegister = false

    private class MyHandler(looper: Looper?) : Handler(looper!!) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            Log.e(TAG, "handleMessage: msg.what:${msg.what}")
            if (msg.what === 1) {
                createNotificationForCustom(mainContext)

                val foreground = isForeground(mainContext)
                Log.e(TAG, "handleMessage: foreground:$foreground, true前台不显示")
                if (!foreground) {
                    removeMessages(1)
                    sendEmptyMessageDelayed(1, 4000)
                }
            }
        }
    }

    fun show(context: Context, from: String) {
        Log.e(TAG, "show: entry index:$index, count:$count, from:$from")
        index = count++ % ALL_COUNT
        mainContext = context.applicationContext
        Log.e(TAG, "show: 2 index:$index, count:$count")

        createReceiver(mainContext)

        handler.removeMessages(1)
        handler.sendEmptyMessage(1)
    }

    fun remove() {
        Log.e(TAG, "remove: ")
        handler.removeCallbacksAndMessages(null)
    }

    fun cancel(context: Context?) {
        Log.e(TAG, "cancel: ")
        if (context != null) {
            val notificationAlert = NotificationAlert()
            notificationAlert.cancelNotification(context)
        }
        remove()
    }

    private fun createNotificationForCustom(context: Context?) {
        if (context != null) {
            Log.e(TAG, "createNotificationForCustom: index:$index")
            val notificationAlert = NotificationAlert()
            notificationAlert.showNotification(context, index)
        }
    }

    private fun createReceiver(context: Context?) {
        Log.e(TAG, "createReceiver: register: status:$isRegister")
        if (isRegister) {
            Log.e(TAG, "createReceiver: have register before")

            return
        }
        val intentFilter = IntentFilter()
        // 添加接收事件监听
        intentFilter.addAction(NOTIFY_DELETE_ACTION)
        var mReceiver = NotificationReceiver()
        // 注册广播
        context?.registerReceiver(mReceiver, intentFilter)
        isRegister = true
        Log.e(TAG, "createReceiver: finish")
    }

    private class NotificationReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            // 拦截接收事件
            Log.e(TAG, "onReceive: action:${intent.action}")
            if (intent.action == NOTIFY_DELETE_ACTION) {
                cancel(context)
                remove()
            }
        }
    }

    private fun isForeground(context: Context?): Boolean {
        if (context == null) {
            return true
        }

        var foreground: Boolean = true
        val appContext: Context = context.applicationContext
        if (appContext is BaseApplication) {
            foreground = appContext.isForeground()
        }
        return foreground
    }
}